/*
 * PcmPlayoutBuffer.h
 *
 *  Created on: Nov 27, 2011
 *      Author: shaohong
 */

#ifndef PCMPLAYOUTBUFFER_H_
#define PCMPLAYOUTBUFFER_H_

#include "PCM_Data_Block.h"

#include <pthread.h>
#include <list>

using namespace std;
namespace nmps {

/**
 * This class represent the audio play-out buffer.
 * The mp3 decoder can use this to dump the result of decoded pcm audio.
 * The RTP streaming source can pick up audio data from this buffer and send it to the streaming client.
 */

class PcmPlayoutBuffer {
public:
	PcmPlayoutBuffer();
	virtual ~PcmPlayoutBuffer();

	/**
	 * save the decoded pcm audio in this buffer.
	 * A side effect that if the buffer is full, the newly added data will
	 * This can be used by the mp3 decoder thread to save the decoded PCM audio data
	 */
	void enQueuePcmDataBlock(int blockSize, unsigned char * pcmData);

	/**
	 * extract the first available pcmDataBlock.
	 * This can be used by the RTP streaming source to retrieve pcm audio data and play it out to the RTP streaming client
	 * It's the caller's responsibility to free the block after use to release the resources.
	 *
	 * The return value maybe NULL which indicates the playout buffer is empty.
	 */
	PCM_Data_Block * deQueuePcmDataBlock();

	/**
	 * Get the number of PCM DataBlocks in the buffer
	 */
	int getNumberOfBlocks();

	/**
	 * return the total number of bytes of the PCM data saved in the playout buffer
	 */
	unsigned long getTotalPCMDataSize();

	/**
	 * clear all the PCM data in this buffer
	 */
	void clearAllData();

private:

	list<PCM_Data_Block *> q;

	int maxNumberOfBlocks;	// shall we set a maximum number of blocks?

};

} /* namespace nmps */
#endif /* PCMPLAYOUTBUFFER_H_ */
